package com.shallowUniverse.dmp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shallowUniverse.dmp.entity.Params;
import com.shallowUniverse.dmp.entity.Result;
import com.shallowUniverse.dmp.pojo.Transfer;
import com.shallowUniverse.dmp.pojo.vo.TransferVo;
import com.shallowUniverse.dmp.service.TransferService;
import com.shallowUniverse.dmp.utils.PageUtil;
import com.shallowUniverse.dmp.utils.ResultTool;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Shuai Guo
 * @version 1.0
 * @description
 * @date 2021/8/5
 */
@RestController
@RequestMapping("/transfers")
public class TransferController {

    @Resource
    private TransferService transferService;

    @GetMapping
    public Result<IPage<Transfer>> findByPage(@RequestBody Params<Transfer> params) {
        //返回数据
        return ResultTool.success(transferService.page(PageUtil.build(params), lambda(params)));
    }

    @GetMapping("/list")
    public Result<List<Transfer>> find(@RequestBody Params<Transfer> params) {
        //返回数据
        return ResultTool.success(transferService.list(lambda(params)));
    }

    @PostMapping
    public Result<Long> save(@RequestBody Params<TransferVo> params) {
        //添加
        transferService.save(params.getBody());
        //返回
        return ResultTool.success(params.getBody().getTransfer().getSid());
    }

    @PutMapping
    public Result<Long> update(@RequestBody Params<Transfer> params) {
        //更新
        transferService.updateById(params.getBody());
        //返回
        return ResultTool.success(params.getBody().getSid());
    }

    @DeleteMapping
    public Result<Long> delete(@RequestBody Params<List<Long>> params) {
        //删除数据源
        boolean result = transferService.removeByIds(params.getBody());
        //返回
        return result ? ResultTool.success("删除成功") : ResultTool.fail("删除失败");
    }

    private LambdaQueryWrapper<Transfer> lambda(Params<Transfer> params) {
        //构造查询条件
        LambdaQueryWrapper<Transfer> lambdaQueryWrapper = new QueryWrapper<Transfer>().lambda();
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(params.getBody().getFromDatabase()), Transfer::getFromDatabase, params.getBody().getFromDatabase());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(params.getBody().getToDatabase()), Transfer::getToDatabase, params.getBody().getToDatabase());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(params.getBody().getFromTable()), Transfer::getFromTable, params.getBody().getFromTable());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(params.getBody().getToTable()), Transfer::getToTable, params.getBody().getToTable());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(params.getBody().getStatus()), Transfer::getStatus, params.getBody().getStatus());
        return lambdaQueryWrapper;
    }
}
